package logic

import (
	"context"
	"errors"
	"furni-suite/furnirpc/furnirpc"
	"furni-suite/furnirpc/internal/svc"
	"furni-suite/models/mysql"
	"github.com/zeromicro/go-zero/core/logx"
)

type FurnitureAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewFurnitureAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *FurnitureAddLogic {
	return &FurnitureAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 家具的添加
func (l *FurnitureAddLogic) FurnitureAdd(in *furnirpc.FurnitureAddRequest) (*furnirpc.FurnitureAddResponse, error) {
	// todo: add your logic here and delete this line
	var furn mysql.Furniture
	if err := furn.FindFurnitureByName(mysql.DB, in.FurnName); err == nil {
		return nil, errors.New("家具不能重复添加")
	}

	findBrand := mysql.Brand{
		Name: in.BrandName,
	}
	if err := findBrand.FindBrandByName(mysql.DB, in.BrandName); err != nil {
		return nil, err
	}

	findClass := mysql.Class{
		Name: in.ClassName,
	}
	if err := findClass.FindClassByName(mysql.DB, in.ClassName); err != nil {
		return nil, err
	}

	furn = mysql.Furniture{
		FurnName: in.FurnName,
		BrandId:  int64(findBrand.ID),
		ClassId:  int64(findClass.ID),
	}
	if err := furn.Furniture(mysql.DB); err != nil {
		return nil, err
	}
	return &furnirpc.FurnitureAddResponse{
		FurnitureId: int64(furn.ID),
	}, nil
}
